package com.henghai.zhenxuan.ui.page.mine;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.henghai.zhenxuan.BR;
import com.henghai.zhenxuan.data.bean.MyTeamBean;
import com.henghai.zhenxuan.domain.request.MyTeamRequester;
import com.henghai.zhenxuan.ui.page.adapter.MyTeamListAdapter;
import com.google.android.material.tabs.TabLayout;
import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;
import com.kongzue.dialogx.dialogs.TipDialog;
import com.kongzue.dialogx.dialogs.WaitDialog;
import com.kunminx.architecture.ui.page.BaseFragment;
import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.kunminx.architecture.ui.page.StateHolder;
import com.kunminx.architecture.ui.state.State;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;

public class TeamFragment extends BaseFragment {

    private TeamStates mStates;
    private MyTeamRequester mMyTeamRequester;
    private int current = 0;
    private RefreshLayout mRefreshLayout;

    @Override
    protected void initViewModel() {
        mStates = getFragmentScopeViewModel(TeamStates.class);
        mMyTeamRequester = getFragmentScopeViewModel(MyTeamRequester.class);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        WaitDialog.show("正在加载");
        mMyTeamRequester.getResult().observe(this, httpBeanDataResult -> {
            WaitDialog.dismiss();
            if (mRefreshLayout != null) {
                mRefreshLayout.finishRefreshWithNoMoreData();
            }
            if (!httpBeanDataResult.getResponseStatus().isSuccess()) {
                TipDialog.show(httpBeanDataResult.getResponseStatus().getResponseCode(), WaitDialog.TYPE.ERROR);
                return;
            }
            mStates.avatar.set(httpBeanDataResult.getResult().getData().getAvatar());
            mStates.userPoints.set("当前积分：" + httpBeanDataResult.getResult().getData().getUserPoints());
            mStates.totalPoints.set("总积分：" + httpBeanDataResult.getResult().getData().getTotalPoints());
            mStates.teamCount.set("团队人数：" + httpBeanDataResult.getResult().getData().getSysUserTeams().size());

            mStates.list.set(httpBeanDataResult.getResult().getData().getSysUserTeams());
        });

        mMyTeamRequester.request(this);
    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        MyTeamListAdapter mAdapter = new MyTeamListAdapter(getContext());
        return new DataBindingConfig(com.henghai.zhenxuan.R.layout.fragment_team, BR.vm, mStates)
                .addBindingParam(BR.listenerTab, new TabListenerHandler())
                .addBindingParam(BR.listenerRefresh, new ListenerRefreshHandler())
                .addBindingParam(BR.listener, new ListenerHandler())
                .addBindingParam(BR.adapter, mAdapter);
    }

    public class TabListenerHandler implements TabLayout.OnTabSelectedListener {

        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            mMyTeamRequester.request(getViewLifecycleOwner());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {
        }
    }

    public class ListenerRefreshHandler implements OnRefreshListener {
        @Override
        public void onRefresh(@NonNull RefreshLayout refreshLayout) {
            mRefreshLayout = refreshLayout;
            mMyTeamRequester.request(getViewLifecycleOwner());
        }
    }


    public class ListenerHandler implements OnTitleBarListener {
        @Override
        public void onLeftClick(TitleBar titleBar) {
            OnTitleBarListener.super.onLeftClick(titleBar);
            nav().navigateUp();
        }

        @Override
        public void onRightClick(TitleBar titleBar) {
            OnTitleBarListener.super.onRightClick(titleBar);
        }
    }

    public static class TeamStates extends StateHolder {
        public final State<ArrayList<MyTeamBean>> list = new State<>(new ArrayList<>());
        public final State<String> teamCount = new State<>("");

        public final State<String> avatar = new State<>("");
        public final State<String> name = new State<>("");
        public final State<String> phone = new State<>("");
        public final State<String> userPoints = new State<>("");
        public final State<String> totalPoints = new State<>("");
    }
}
